package com.example.todolist.controller;

import com.example.todolist.entity.ReviewTask;
import com.example.todolist.service.ReviewTaskService;
import com.example.todolist.tools.ResponseBody;
import com.example.todolist.tools.StringToDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class ReviewTaskController {
    @Autowired
    private ReviewTaskService reviewTaskService;

    public StringToDateConverter stringToDateConverter =new StringToDateConverter();
    @GetMapping("/getOneDayTask")
    public ResponseBody<List<ReviewTask>> getOneDayTask(@RequestParam Integer userId,@RequestParam String date)
    {
        return reviewTaskService.getOneDayTask(userId,stringToDateConverter.StringToDate(date));
    }

    @PostMapping("/insertReviewTask")
    public ResponseBody<Integer> insertReviewTask(@RequestParam Integer userId,@RequestBody ReviewTask reviewTask, @RequestParam String beginDate, @RequestParam String endDate)
    {
        return reviewTaskService.addReviewTask(reviewTask,stringToDateConverter.StringToDate(beginDate),stringToDateConverter.StringToDate(endDate));
    }

    @DeleteMapping("/deleteOneTask")
    public ResponseBody<Integer> deleteOneTask(@RequestParam Integer userId, @RequestParam String date)
    {
        return reviewTaskService.deleteOneTask(userId,stringToDateConverter.StringToDate(date));
    }

    @PostMapping("/checkForReviewTask")
    public ResponseBody<Integer> checkForReviewTask(@RequestParam Integer userId,@RequestParam String date,@RequestParam String taskName)
    {
        Date now = Date.valueOf(LocalDate.now());
        Date p = stringToDateConverter.StringToDate(date);
        if(p.getTime()>now.getTime())
        {
            return new ResponseBody<>(401,"你不能提前打卡",null);
        }
        return reviewTaskService.checkForReviewTask(userId,stringToDateConverter.StringToDate(date),taskName);
    }

}
